var express = require('express');
var router = express.Router();
var fs = require('fs');
var MongoClient = require('mongodb').MongoClient;
var DB_CONN_STR = "mongodb://localhost:27017/app";

/* GET home page. */
router.get('/', function(req, res, next) {
  res.render('index', { title: 'Express' });
});

router.get('/initproject',function(req,res,next){
    fs.readFile('../mock/project.json','utf-8',(err,data)=>{
        var jData=data;
        MongoClient.connect(DB_CONN_STR,function(err,db){
          if(err){
            return;
          }else{
            // console.log(typeof(eval(jData)));
            // var obj = JSON.parse(jData);
            // console.log(typeof(obj));
            
            var conn = db.collection('project');
            conn.insert(eval(jData),function(err,results){
              if(err){
                return;
              }else{
                res.send('插入数据成功！');
              }
            })
          }
        })
    }) 
})

router.get('/initskill',function(req,res,next){
    fs.readFile('../mock/skill.json','utf-8',(err,data)=>{
        var jData=data;
        MongoClient.connect(DB_CONN_STR,function(err,db){
          if(err){
            return;
          }else{
            var conn = db.collection('skill');
            conn.insert(eval(jData),function(err,results){
              if(err){
                return;
              }else{
                res.send('插入数据成功！');
              }
            })
          }
        })
    }) 
})

router.get('/initwork',function(req,res,next){
    fs.readFile('../mock/work.json','utf-8',(err,data)=>{
        var jData=data;
        MongoClient.connect(DB_CONN_STR,function(err,db){
          if(err){
            return;
          }else{
            var conn = db.collection('work');
            conn.insert(eval(jData),function(err,results){
              if(err){
                return;
              }else{
                res.send('插入数据成功！');
              }
            })
          }
        })
    }) 
})

router.get('/initme',function(req,res,next){
    fs.readFile('../mock/me.json','utf-8',(err,data)=>{
        var jData=data;
        MongoClient.connect(DB_CONN_STR,function(err,db){
          if(err){
            return;
          }else{
            var conn = db.collection('me');
            conn.insert(eval(jData),function(err,results){
              if(err){
                return;
              }else{
                res.send('插入数据成功！');
              }
            })
          }
        })
    }) 
})

router.post('/getproject',function(req,res,next){
  MongoClient.connect(DB_CONN_STR,function(err,db){
    if(err){
      return;
    }else{
      var conn = db.collection('project');
      conn.find().toArray(function(err,results){
        if(err){
          return;
        }else{
          res.json({data:results});
        }
      })
    }
  })
})

router.post('/getskill',function(req,res,next){
  MongoClient.connect(DB_CONN_STR,function(err,db){
    if(err){
      return;
    }else{
      var conn = db.collection('skill');
      conn.find().toArray(function(err,results){
        if(err){
          return;
        }else{
          res.json({data:results});
        }
      })
    }
  })
})

router.post('/getwork',function(req,res,next){
  MongoClient.connect(DB_CONN_STR,function(err,db){
    if(err){
      return;
    }else{
       var conn = db.collection('work');
       conn.find().toArray(function(err,results){
        if(err){
          return;
        }else{
          res.json({data:results});
        }
       })
    }
  })
})

router.post('/getme',function(req,res,next){
  MongoClient.connect(DB_CONN_STR,function(err,db){
    if(err){
      return;
    }else{
       var conn = db.collection('me');
       conn.find().toArray(function(err,results){
        if(err){
          return;
        }else{
          res.json({data:results});
        }
       })
    }
  })
})
module.exports = router;
